    /*********************************************************************************************************/
   /* Foreign Borrowing, Sovereignty, and Public Opinion in the Global South: Traditional Lenders or China? */
  /*********************************************************************************************************/
 /******************************* David Bulman, Ning Leng, and Kerry Ratigan ******************************/
/*********************************************************************************************************/

/* 
This file cleans and reshapes the baseline data for conjoint analysis of project loan preferences. It uses the following dataset:
	--- ForeignBorrowingPublicOpinion_CleanData.dta
*/

/* SET DIRECTORY */
cd "C:\Users\dbulman1\Dropbox\Work in progress\Global China Perceptions Survey\Debt\Data and analysis"

/* LOAD DATA */
use ForeignBorrowingPublicOpinion_CleanData.dta, clear

/* Reshape data */

forvalues i=1/5 {
	rename debt_conjoint1_`i' choice`i'
	}

reshape long choice, i(responseid) j(task)
gen rating1=.
gen rating2=.
reshape long rating, i(responseid task) j(loan)
drop rating

gen selected=1 if loan==choice
replace selected=0 if loan!=choice



/* Translate attributes names */

/* Spanish */
forvalues i=1/5 {
	forvalues j=1/5 {
		replace d1c`i'_attrib`j'_name="This loan is for:" if d1c`i'_attrib`j'_name=="Este préstamo es para construir una:"
		replace d1c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d1c`i'_attrib`j'_name=="Si su gobierno no puede pagar este préstamo, tendrán que pagar a los acreedores con:"
		replace d1c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d1c`i'_attrib`j'_name=="Para conseguir este préstamo, el acreedor requiere que su gobierno:"
		replace d1c`i'_attrib`j'_name="The loan is provided by:" if d1c`i'_attrib`j'_name=="El préstamo será proporcionado por:"
		replace d1c`i'_attrib`j'_name="The interest rate for this loan is:" if d1c`i'_attrib`j'_name=="La tasa de interés para este préstamo es:" 
		}
	}

/*Indonesian*/
forvalues i=1/5 {
	forvalues j=1/5 {
		replace d1c`i'_attrib`j'_name="This loan is for:" if d1c`i'_attrib`j'_name=="Pinjaman ini untuk:"
		replace d1c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d1c`i'_attrib`j'_name=="Jika pemerintah Anda tidak dapat membayar kembali pinjaman ini, mereka akan membayar kembali pemberi pinjaman dengan:"
		replace d1c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d1c`i'_attrib`j'_name=="Untuk mendapatkan pinjaman ini, pemberi pinjaman mengharuskan pemerintah Anda untuk:"
		replace d1c`i'_attrib`j'_name="The loan is provided by:" if d1c`i'_attrib`j'_name=="Pinjaman ini disediakan oleh:"
		replace d1c`i'_attrib`j'_name="The interest rate for this loan is:" if d1c`i'_attrib`j'_name=="Tingkat bunga untuk pinjaman ini adalah:" 
		}
	}

/* Malaysian */	
forvalues i=1/5 {
	forvalues j=1/5 {
		replace d1c`i'_attrib`j'_name="This loan is for:" if d1c`i'_attrib`j'_name=="Pinjaman ini adalah untuk:"
		replace d1c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d1c`i'_attrib`j'_name=="Jika kerajaan anda tidak dapat membayar balik pinjaman ini, mereka akan sebaliknya membayar balik pemberi pinjaman dengan:"
		replace d1c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d1c`i'_attrib`j'_name=="Untuk mendapatkan pinjaman ini, pemberi pinjaman memerlukan kerajaan anda untuk:"
		replace d1c`i'_attrib`j'_name="The loan is provided by:" if d1c`i'_attrib`j'_name=="Pinjaman ini disediakan oleh:"
		replace d1c`i'_attrib`j'_name="The interest rate for this loan is:" if d1c`i'_attrib`j'_name=="Kadar faedah untuk pinjaman ini adalah:" 
		}
	}

/* Chinese */
forvalues i=1/5 {
	forvalues j=1/5 {
		replace d1c`i'_attrib`j'_name="This loan is for:" if d1c`i'_attrib`j'_name=="这项贷款的项目是:"
		replace d1c`i'_attrib`j'_name="If your government cannot repay this loan, they will instead repay the lenders with:" if d1c`i'_attrib`j'_name=="如果你的政府无力偿还该项贷款，后果如下:"
		replace d1c`i'_attrib`j'_name="In order to get this loan, the lenders require your government to:" if d1c`i'_attrib`j'_name=="想要得到这笔贷款，贷款方提出如下附带条款:"
		replace d1c`i'_attrib`j'_name="The loan is provided by:" if d1c`i'_attrib`j'_name=="贷款方为:"
		replace d1c`i'_attrib`j'_name="The interest rate for this loan is:" if d1c`i'_attrib`j'_name=="贷款利率为:" 
		}
	}
	
	
/* Match and encode project attributes */

gen purpose=""
gen collateral=""
gen condition=""
gen lender=""
gen intrate=""

forvalues i=1/5 {
	forvalues j=1/5 {
		forvalues k=1/2 {
			replace purpose=d1c`i'_attrib`j'_project`k' if d1c`i'_attrib`j'_name=="This loan is for:" & task==`i' & loan==`k'
			replace collateral=d1c`i'_attrib`j'_project`k' if d1c`i'_attrib`j'_name=="If your government cannot repay this loan, they will instead repay the lenders with:" & task==`i' & loan==`k'
			replace condition=d1c`i'_attrib`j'_project`k' if d1c`i'_attrib`j'_name=="In order to get this loan, the lenders require your government to:" & task==`i' & loan==`k'
			replace lender=d1c`i'_attrib`j'_project`k' if d1c`i'_attrib`j'_name=="The loan is provided by:" & task==`i' & loan==`k'
			replace intrate=d1c`i'_attrib`j'_project`k' if d1c`i'_attrib`j'_name=="The interest rate for this loan is:" & task==`i' & loan==`k'
			}
		}
	}

replace intrate="2%" if intrate==".02"
replace intrate="6%" if intrate==".06"
replace intrate="10%" if intrate==".1"
	
/* Translate attributes */
	
	/*Spanish*/
	replace purpose="Highway network" if purpose=="Red de carreteras"
	replace purpose="5G telecommunications network" if purpose=="Red de telecomunicaciones 5G"
	replace collateral="Natural resources at a below market price" if collateral=="Recursos naturales a un precio inferior al del mercado  "
	replace collateral="Exclusive rights to a large area of land" if collateral=="Derechos exclusivos sobre un terreno grande"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Nada, pero perderán oportunidades para préstamos futuros"	
	replace condition="Increase transparency in government spending" if condition=="Mejore la transparencia de los gastos gubernamentales"	
	replace condition="Cut government spending on public programs" if condition=="Reduzca los gastos gubernamentales para programas públicos"
	replace condition="Use labor and materials from the lender's country to build the project" if condition=="Use mano de obra y materia del país del acreedor para construir el proyecto"
	replace condition="No conditions specified" if condition=="Ninguna condición"
	replace lender="The United States government" if lender=="El gobierno de los Estados Unidos"
	replace lender="The Chinese government" if lender==" El gobierno de China"
	replace lender="The Chinese government" if lender=="El gobierno de China"
	replace lender="Private commercial banks" if lender=="Bancos privados comerciales"
	replace lender="The World Bank" if lender=="El Banco Mundial"

	/*Indonesian*/
	replace purpose="Highway network" if purpose=="Jaringan jalan raya"
	replace purpose="5G telecommunications network" if purpose=="Jaringan telekomunikasi 5G"	
	replace collateral="Natural resources at a below market price" if collateral=="Sumber daya alam dengan harga di bawah pasar"
	replace collateral="Exclusive rights to a large area of land" if collateral=="Hak eksklusif atas area lahan yang luas"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Tidak ada, tetapi kehilangan peluang peminjaman di masa depan"	
	replace condition="Increase transparency in government spending" if condition=="Meningkatkan transparansi dalam pengeluaran pemerintah"	
	replace condition="Cut government spending on public programs" if condition=="Mengurangi pengeluaran pemerintah untuk program publik"
	replace condition="Use labor and materials from the lender's country to build the project" if condition=="Menggunakan tenaga kerja dan bahan dari negara pemberi pinjaman untuk membangun proyek"	
	replace condition="No conditions specified" if condition=="Tidak ada kondisi yang ditentukan"
	replace lender="The United States government" if lender=="Pemerintah Amerika Serikat"
	replace lender="The Chinese government" if lender=="Pemerintah Tiongkok"
	replace lender="Private commercial banks" if lender=="Bank komersial swasta"
	replace lender="The International Monetary Fund (IMF)" if lender=="Dana Moneter Internasional (IMF)"

	/*Malaysian*/
	replace purpose="Highway network" if purpose=="Rangkaian lebuh raya"
	replace purpose="5G telecommunications network" if purpose=="Rangkaian telekomunikasi 5G"	
	replace collateral="Natural resources at a below market price" if collateral=="Sumber asli pada harga di bawah pasaran"
	replace collateral="Exclusive rights to a large area of land" if collateral=="Hak eksklusif ke atas kawasan tanah yang luas"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="Tiada, tetapi kehilangan peluang pinjaman masa depan"	
	replace condition="Increase transparency in government spending" if condition=="Meningkatkan ketelusan dalam perbelanjaan kerajaan"
	replace condition="Cut government spending on public programs" if condition=="Mengurangkan perbelanjaan kerajaan untuk program awam"
	replace condition="Use labor and materials from the lender's country to build the project" if condition=="Menggunakan tenaga kerja dan bahan dari negara pemberi pinjaman untuk membina projek"	
	replace condition="No conditions specified" if condition=="Tiada syarat yang ditetapkan"
	replace lender="The United States government" if lender=="Kerajaan Amerika Syarikat"
	replace lender="The Chinese government" if lender=="Kerajaan China"
	replace lender="Private commercial banks" if lender=="Bank komersial swasta"
	replace lender="The International Monetary Fund (IMF)" if lender=="Tabung Kewangan Antarabangsa (IMF)"

	/*Chinese*/
	replace purpose="Highway network" if purpose=="高速公路网"
	replace purpose="5G telecommunications network" if purpose=="5G电信通讯网络"	
	replace collateral="Natural resources at a below market price" if collateral=="借款方以低于市场价格获取自然资源"
	replace collateral="Exclusive rights to a large area of land" if collateral=="借款方获得大片土地的专有权"	
	replace collateral="Nothing, but lose future borrowing opportunities" if collateral=="没具体后果，但会失去以后借款的机会"	
	replace condition="Increase transparency in government spending" if condition=="提高政府支出的透明度"
	replace condition="Cut government spending on public programs" if condition=="减少政府在公共项目上的开支"
	replace condition="Use labor and materials from the lender's country to build the project" if condition=="用来自于贷款国家的劳工和原料来修建项目"	
	replace condition="No conditions specified" if condition=="没有附带条款"
	replace lender="The United States government" if lender=="美国政府"
	replace lender="The Chinese government" if lender=="中国政府"
	replace lender="The Chinese government" if lender==" 中国政府"
	replace lender="Private commercial banks" if lender=="商业银行"
	replace lender="The International Monetary Fund (IMF)" if lender=="国际货币基金组织"
		
	
/* Give shorter labels */
gen purpose2="5G network" if purpose=="5G telecommunications network"
replace purpose2="Highway network" if purpose=="Highway network"
gen collateral2="Land" if collateral=="Exclusive rights to a large area of land"
replace collateral2="Resources" if collateral=="Natural resources at a below market price"
replace collateral2="Nothing" if collateral=="Nothing, but lose future borrowing opportunities"
gen condition2="Transparency" if condition=="Increase transparency in government spending"
replace condition2="Transparency" if condition=="Increase transparency in government spending"
replace condition2="Austerity" if condition=="Cut government spending on public programs"
replace condition2="Labor and materials" if condition=="Use labor and materials from the lenderâ€™s country to build the project"
replace condition2="Labor and materials" if condition=="Use labor and materials from the lender's country to build the project"
replace condition2="None" if condition=="No conditions specified"
gen lender2="Private banks" if lender=="Private commercial banks"
replace lender2="Chinese government" if lender=="The Chinese government"
replace lender2="US government" if lender=="The United States government"
replace lender2="World Bank" if lender=="The World Bank"
gen intrate2=intrate

/* Numerically encode attributes and specify mapping that we want*/
label define labels_purpose /// 
		   1 "Highway network" ///
           2 "5G network"
label define labels_collateral ///
           1 "Nothing" ///
           2 "Land" ///
           3 "Resources"
label define labels_condition ///
           1 "None" ///
           2 "Austerity" ///
		   3 "Transparency" ///
		   4 "Labor and materials"
label define labels_lender ///
           1 "World Bank" ///
           2 "Chinese government" ///
		   3 "US government" ///
		   4 "Private banks"
label define labels_intrate ///
           1 "2%" ///
           2 "6%" ///
		   3 "10%" 

encode purpose2, gen(att_purpose) label(labels_purpose)
encode collateral2, gen(att_collateral) label(labels_collateral)
encode condition2, gen(att_condition) label(labels_condition)
encode lender2, gen(att_lender) label(labels_lender)
encode intrate2, gen(att_intrate) label(labels_intrate) 

drop purpose* collateral* condition* lender* intrate* *_name *_project1 *_project2	

save ForeignBorrowingPublicOpinion_ProjectLoan.dta, replace